//
//  ChatHistoryCell.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-7-7.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "ChatHistoryCell.h"
#import "UIAlertView+Kirinrin.h"
#import "NSDate+Category.h"
#import "UIImageView+AFNetworking.h"


@implementation ChatHistoryCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setData:(ParentInfo *)data{
    [_name setText: data.name];
    _content.text = [self subTitleMessageByConversation:data.messages];
    
    _chatDate.text = [self lastMessageTimeByConversation:data.messages];
//    cell.unreadCount = [self unreadMessageCountByConversation:data];
    [_photo setImageWithURL:data.photo placeholderImage:[UIImage imageNamed:@"imageUserPhotoDefault"]];
}

// 得到最后消息时间
-(NSString *)lastMessageTimeByConversation:(EMConversation *)conversation
{
    NSString *ret = @"";
    EMMessage *lastMessage = [conversation latestMessage];
    if (lastMessage) {
        ret = [NSDate formattedTimeFromTimeInterval:lastMessage.timestamp];
    }
    
    return ret;
}

// 得到未读消息条数
- (NSInteger)unreadMessageCountByConversation:(EMConversation *)conversation
{
    NSInteger ret = 0;
    ret = conversation.unreadMessagesCount;
    
    return  ret;
}

// 得到最后消息文字或者类型
-(NSString *)subTitleMessageByConversation:(EMConversation *)conversation
{
    NSString *ret = @"";
    EMMessage *lastMessage = [conversation latestMessage];
    if (lastMessage) {
        id<IEMMessageBody> messageBody = lastMessage.messageBodies.lastObject;
        switch (messageBody.messageBodyType) {
            case eMessageBodyType_Image:{
                ret = @"[图片]";
            }
                break;
            case eMessageBodyType_Text:{
                ret = ((EMTextMessageBody *)messageBody).text;
            }
                break;
            case eMessageBodyType_Voice:{
                ret = @"[声音]";
            }
                break;
            case eMessageBodyType_Location:
                ret = @"[位置]";
            default:
                break;
        }
    }
    
    return ret;
}
@end
